package com.moneycollect.webspider.common;

import java.io.*;

/**
 * @author xuhuiqian
 * @since 2021/11/30 10:35
 */
public class FileHelper {

    public static String readAllFile4Utf8(String fileName) throws IOException {
        String encoding = "UTF-8";
        File file = new File(fileName);
        long filelength = file.length();
        byte[] filecontent = new byte[(int) filelength];

        FileInputStream in = new FileInputStream(file);
        var r = in.read(filecontent);
        in.close();
        if (r <= 0) {
            return "";
        }
        return new String(filecontent, encoding);
    }

    public static void saveFile(String content, String fileName) throws Exception {
        File file = new File(fileName);
        if(!file.exists()) {
            file.createNewFile();
        }
        BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));
        writer.write(content);
        writer.close();
    }
}
